package xin.marcher.module.search.message;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xin.marcher.module.search.domain.dto.TableDataChangeDTO;

import java.io.IOException;
import java.util.List;

/**
 *
 */
@Component
public class MessageHandlerManager {

    @Autowired
    private List<MessageHandler> messageHandlers;

    public void handleMessage(TableDataChangeDTO tableDataChangeDTO) throws IOException {
        MessageHandler messageHandlerToUse = messageHandlers.stream()
                .filter(e -> StringUtils.equals(e.tableName(), tableDataChangeDTO.getTableName()))
                .findFirst()
                .orElse(null);
        if (messageHandlerToUse == null) {
            return;
        }
        messageHandlerToUse.handleMessage(tableDataChangeDTO);
    }
}
